//
//  RGLoginResetPasswordView.m
//  RedGrass
//
//  Created by wukong on 2019/1/2.
//  Copyright © 2019年 hongcaosp. All rights reserved.
//

#import "RGLoginResetPasswordView.h"
#import "CountDownControl.h"

@interface RGLoginResetPasswordView()

@property (strong, nonatomic) UIView *registView;
@property (strong, nonatomic) UIScrollView *scrollView;
@property (strong, nonatomic) UIView *containerView;
@property (strong, nonatomic) UIImageView *bgImg;
@property (strong, nonatomic) UILabel *alertLabel;
@property (strong, nonatomic) UIView *bgView1;
@property (strong, nonatomic) UIImageView *bgImg1;
@property (strong, nonatomic) UITextField *captchaTF;
@property (strong, nonatomic) UIView *bgView2;
@property (strong, nonatomic) UIImageView *bgImg2;
@property (strong, nonatomic) UITextField *passwordTF;
@property (strong, nonatomic) UIView *bgView3;
@property (strong, nonatomic) UIImageView *bgImg3;
@property (strong, nonatomic) UIButton *captchaBtn;
@property (strong, nonatomic) UILabel *countLabel;
@property (strong, nonatomic) UIButton *loginBtn;



@end

@implementation RGLoginResetPasswordView

-(instancetype) initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        
        [self initUI];
        [self initRegistViewUI];
    }
    return self;
}

-(void)initUI{
    
    [self addSubview:self.bgImg];
    [_bgImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
    [self addSubview:self.registView];
    [_registView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
}

//UI
-(void)initRegistViewUI{
    
    [_registView addSubview:self.scrollView];
    [self.scrollView addSubview:self.containerView];
    [self registViewAddMasonry];
    [self initRegisterContainerView];
}

-(void) registViewAddMasonry{
    
    [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
    [_containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
        make.width.mas_equalTo(self.scrollView).mas_offset(0);
        make.height.mas_greaterThanOrEqualTo(KScreenHeight);
    }];
}

-(void) initRegisterContainerView{
    AA_WEAK_MAKE(self)weakSelf = self;
    [_containerView addSubview:self.alertLabel];
    [_alertLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(weakSelf.containerView.mas_top).mas_offset(310);
        make.centerX.mas_equalTo(weakSelf.containerView).mas_offset(0);
        make.height.mas_equalTo(30);
    }];
    
    [_containerView addSubview:self.bgView3];
    [_bgView3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.alertLabel.mas_bottom).mas_offset(12);
//        make.top.mas_equalTo(weakSelf.bgView1.mas_bottom).mas_offset(4);
        make.right.mas_equalTo(weakSelf.containerView).mas_offset(-40);
        make.height.mas_equalTo(60);
        make.width.mas_equalTo(120);
    }];
    [_containerView addSubview:self.bgView2];
    [_bgView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.alertLabel.mas_bottom).mas_offset(12);
        make.left.mas_equalTo(weakSelf.containerView).mas_offset(40);
        make.height.mas_equalTo(60);
        make.right.mas_equalTo(weakSelf.bgView3.mas_left).mas_offset(8);
    }];
    
    
    [_containerView addSubview:self.bgView1];
    [_bgView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.bgView3.mas_bottom).mas_offset(4);
//        make.top.mas_equalTo(weakSelf.alertLabel.mas_bottom).mas_offset(12);
        make.centerX.mas_equalTo(weakSelf.containerView).mas_offset(0);
        make.left.mas_equalTo(weakSelf.containerView).mas_offset(40);
        make.height.mas_equalTo(60);
    }];
    
    [_containerView addSubview:self.loginBtn];
    [_loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.bgView1.mas_bottom).mas_offset(12);
        make.centerX.mas_equalTo(weakSelf.containerView).mas_offset(0);
        make.left.mas_equalTo(weakSelf.containerView).mas_offset(48);
        make.bottom.mas_equalTo(weakSelf.containerView.mas_bottom).mas_offset(-60).with.priority(250);;
        make.height.mas_equalTo(44);
    }];
    
    [self initBgView2];
    [self initBgView3];
    [self initBgView1];
    
}

-(void)initBgView1{
    [_bgView1 addSubview:self.bgImg1];
    [_bgImg1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
    AA_WEAK_MAKE(self) weakSelf = self;
    [_bgView1 addSubview:self.passwordTF];
    [_passwordTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.bgView1).mas_offset(-4);
        make.left.mas_equalTo(weakSelf.bgView1).mas_offset(22);
        make.height.mas_equalTo(26);
        make.right.mas_equalTo(weakSelf.bgView1).mas_offset(-8);
    }];
}

-(void)initBgView2{
    [_bgView2 addSubview:self.bgImg2];
    [_bgImg2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
    AA_WEAK_MAKE(self) weakSelf = self;
    [_bgView2 addSubview:self.captchaTF];
    [_captchaTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.bgView2).mas_offset(-4);
        make.left.mas_equalTo(weakSelf.bgView2).mas_offset(22);
        make.height.mas_equalTo(26);
        make.right.mas_equalTo(weakSelf.bgView2).mas_offset(-12);
    }];
}

-(void)initBgView3{
    [_bgView3 addSubview:self.bgImg3];
    [_bgImg3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
    
    AA_WEAK_MAKE(self) weakSelf = self;
    [_bgView3 addSubview:self.countLabel];
    [_countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.bgView3).mas_offset(-4);
        make.centerX.mas_equalTo(weakSelf.bgView3).mas_offset(0);
    }];
    
    [_bgView3 addSubview:self.captchaBtn];
    [_captchaBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.bgView3).mas_offset(-4);
        make.centerX.mas_equalTo(weakSelf.bgView3).mas_offset(0);
    }];
}

#pragma mark - response

#pragma mark - private
-(void) viewResignFirstReponder{
    [_captchaTF resignFirstResponder];
    [_passwordTF resignFirstResponder];
}

-(void)captchaBtnClick:(UIButton *)button{
    [self viewResignFirstReponder];
    if([self.viewDelegate respondsToSelector:@selector(gainCaptchaCodeWithView:)]){
        [self.viewDelegate gainCaptchaCodeWithView:self];
    }
}

-(void) loginBtnClick:(UIButton *)button{
    [self viewResignFirstReponder];
    if([self.viewDelegate respondsToSelector:@selector(resetPassWithView:code:pass:)]){
        [self.viewDelegate resetPassWithView:self code:_captchaTF.text pass:_passwordTF.text];
    }
}


#pragma mark - public
//开始倒计时
-(void) begainGainCodeTimer{
    CountDownControl *countCTL = [[CountDownControl alloc]init];
    countCTL.countLabel = _countLabel;
    countCTL.operationView = _captchaBtn;
    [countCTL start:60];
}


//-(NSString *)captchaCode{
//    return _captchaTF.text;
//}

#pragma mark - getter


-(UIView *)registView{
    if(!_registView){
        _registView = [[UIView alloc]init];
    }
    return _registView;
}


-(UIScrollView *)scrollView{
    if(!_scrollView){
        _scrollView = [[UIScrollView alloc]init];
    }
    return _scrollView;
}

-(UIView *)containerView{
    if(!_containerView){
        _containerView = [[UIView alloc]init];
    }
    return _containerView;
}

-(UIImageView *)bgImg{
    if(!_bgImg){
        _bgImg = [[UIImageView alloc]init];
        _bgImg.image = IMAGE_NAMED(@"bj");
        //        _bgImg.contentMode = UIViewContentModeCenter;
    }
    return _bgImg;
}

-(UILabel *)alertLabel{
    if(!_alertLabel){
        _alertLabel = [[UILabel alloc]init];
        _alertLabel.textColor = CFontColor2;
        _alertLabel.text = @"手机号登录有助于您快速找到朋友";
        _alertLabel.font = FONT(customFontName, 14);
        _alertLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _alertLabel;
}


-(UIView *)bgView1{
    if(!_bgView1){
        _bgView1 = [[UIView alloc]init];
    }
    return _bgView1;
}

-(UIImageView *)bgImg1{
    if(!_bgImg1){
        _bgImg1 = [[UIImageView alloc]init];
        _bgImg1.image = IMAGE_NAMED(@"框1");
    }
    return _bgImg1;
}


-(UIView *)bgView2{
    if(!_bgView2){
        _bgView2 = [[UIView alloc]init];
    }
    return _bgView2;
}



-(UIImageView *)bgImg2{
    if(!_bgImg2){
        _bgImg2 = [[UIImageView alloc]init];
        _bgImg2.image = IMAGE_NAMED(@"框2");
    }
    return _bgImg2;
}


-(UIView *)bgView3{
    if(!_bgView3){
        _bgView3 = [[UIView alloc]init];
    }
    return _bgView3;
}

-(UIImageView *)bgImg3{
    if(!_bgImg3){
        _bgImg3 = [[UIImageView alloc]init];
        _bgImg3.image = IMAGE_NAMED(@"框3");
    }
    return _bgImg3;
}


-(UITextField *)captchaTF{
    if(!_captchaTF){
        _captchaTF = [[UITextField alloc]init];
        _captchaTF.tintColor = UICOLOR_WITH_RGBINT(0x4da8fc);
        _captchaTF.textColor = CFontColor1;
        _captchaTF.font = FONT(customFontName, FFont_14);
        _captchaTF.placeholder = @"请输入短信验证码";
//        _captchaTF.keyboardType = UIKeyboardTypeNumberPad;
        
    }
    return _captchaTF;
}


-(UITextField *)passwordTF{
    if(!_passwordTF){
        _passwordTF = [[UITextField alloc]init];
        _passwordTF.tintColor = UICOLOR_WITH_RGBINT(0x4da8fc);
        _passwordTF.textColor = CFontColor1;
        _passwordTF.font = FONT(customFontName, FFont_14);
        _passwordTF.placeholder = @"请输入8-20位新密码";
    }
    return _passwordTF;
}

-(UIButton *)captchaBtn{
    if(!_captchaBtn){
        _captchaBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_captchaBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        [_captchaBtn setTitleColor:UICOLOR_WITH_RGBINT(0x62a0fe) forState:UIControlStateNormal];
        [_captchaBtn.titleLabel setFont:FONT(customFontName, FFont_14)];
        [_captchaBtn addTarget:self action:@selector(captchaBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _captchaBtn;
}

-(UILabel *)countLabel{
    if(!_countLabel){
        _countLabel = [[UILabel alloc]init];
        _countLabel.text = @"剩余60秒";
        _countLabel.textColor = UICOLOR_WITH_RGBINT(0x62a0fe);
        _countLabel.font = FONT(customFontName, FFont_14);
        _countLabel.hidden = YES;
    }
    return _countLabel;
}

-(UIButton *)loginBtn{
    if(!_loginBtn){
        _loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_loginBtn setBackgroundImage:IMAGE_NAMED(@"登录") forState:UIControlStateNormal];
        [_loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
        [_loginBtn.titleLabel setFont:FONT(customFontName, 15)];
        [_loginBtn addTarget:self action:@selector(loginBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginBtn;
}


@end
